package com.gitee.slowcreator.http.file.download.okhttp;

import com.gitee.slowcreator.http.file.download.template.AbstractByteFileDownload;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * okhttp 的 byte 形式文件下载
 *
 * @author zxw
 * @date 2023/6/10 15:40
 */
@Slf4j
public class OkhttpByteFileDownload extends AbstractByteFileDownload<Request, Response> {

    @Override
    @SneakyThrows
    protected byte[] getByteData(Request request, Response response) {

        ResponseBody body = response.body();
        if (body == null) {
            log.info("请求下载url:{},返回数据为空！", response.request().url());
            return null;
        }
        return body.bytes();
    }

    @Override
    protected String getContentDisposition(Request request, Response response) {

        return response.header("Content-Disposition");
    }

    @Override
    protected String getRequestUrl(Request request, Response response) {

        return response.request().url().toString();
    }
}
